package com.csd.fragment;

import com.csd.activity.PaymentActivity;
import com.csd.activity.PersonalDetailActivity;
import com.csd.activity.R;
import com.csd.bean.Car;
import com.csd.bean.EnterpriseOrder;
import com.csd.bean.PersonalOrder;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class PayFragment extends Fragment implements OnClickListener {

	private View view;
	private TextView tv_orderId, tv_carBrand, tv_plate, tv_taketime,
			tv_returntime, tv_state, tv_payment, tv_txt;
	private ImageView img_car;
	private LinearLayout ll_detail, ll_bt,ll_cancle;
	private Button bt_no,bt_yes;

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		view = inflater.inflate(R.layout.fragment_pay, null);
		init();
		return view;
	}

	private void init() {
		// TODO Auto-generated method stub
		ll_cancle = (LinearLayout) view.findViewById(R.id.ll_cancle);
		tv_orderId = (TextView) view.findViewById(R.id.orderid);
		tv_state = (TextView) view.findViewById(R.id.state);
		tv_carBrand = (TextView) view.findViewById(R.id.carbrand);
		tv_plate = (TextView) view.findViewById(R.id.plate);
		tv_taketime = (TextView) view.findViewById(R.id.taketime);
		tv_returntime = (TextView) view.findViewById(R.id.returntime);
		tv_txt = (TextView) view.findViewById(R.id.pay_txt);
		tv_payment = (TextView) view.findViewById(R.id.payment);
		img_car = (ImageView) view.findViewById(R.id.carimg);
		ll_detail = (LinearLayout) view.findViewById(R.id.detail);
		ll_detail.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						PersonalDetailActivity.class);
				startActivity(intent);
			}
		});
		LinearLayout.LayoutParams params = new 	LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
		params.setMargins(10, 10, 10, 10);
		PersonalOrder order = new PersonalOrder();
		order.setOrderId("CWDD-0035");
		Car car = new Car();
		car.setImg(R.drawable.u120);
		car.setCarBrand("丰田威驰");
		car.setPlate("沪A-3245");
		order.setCar(car);
		order.setTakeTime("2014/11/19 18:00");
		order.setReturnTime("2014/11/19 20:00");
		order.setPayment(150);
		order.setState("待付款");
		ll_bt = (LinearLayout) view.findViewById(R.id.ll_bt);
		Button bt_pay = new Button(getActivity());
		bt_pay.setText("付款");
		bt_pay.setTextColor(Color.parseColor("#ffffff"));
		bt_pay.setLayoutParams(params);
		bt_pay.setBackgroundResource(R.drawable.bt_orange);
		bt_pay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), PaymentActivity.class);
				getActivity().startActivity(intent);
			}
		});
		Button bt_revise = new Button(getActivity());
		bt_revise.setText("修改订单");
		bt_revise.setBackgroundResource(R.drawable.bt_green);
		bt_revise.setTextColor(Color.parseColor("#ffffff"));
		bt_revise.setLayoutParams(params);
		bt_revise.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(getActivity(), "修改订单", Toast.LENGTH_SHORT)
						.show();
			}
		});
		Button bt_cancle = new Button(getActivity());
		bt_cancle.setText("取消订单");
		bt_cancle.setTextColor(Color.parseColor("#ffffff"));
		bt_cancle.setBackgroundResource(R.drawable.bt_red);
		bt_cancle.setLayoutParams(params);
		bt_cancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ll_cancle.setVisibility(View.VISIBLE);
				DisplayMetrics dm = new DisplayMetrics();
				dm = getActivity().getApplicationContext().getResources()
						.getDisplayMetrics();
				int screenHeight = dm.heightPixels;
				AnimationSet animationSet = new AnimationSet(true);
				TranslateAnimation animation = new TranslateAnimation(0, 0,
						screenHeight, 0);
				animation.setDuration(1000);
				animationSet.addAnimation(animation);
				ll_cancle.startAnimation(animationSet);

			}
		});
		ll_bt.addView(bt_pay);
		ll_bt.addView(bt_revise);
		ll_bt.addView(bt_cancle);
		tv_orderId.setText(order.getOrderId());
		tv_state.setText(order.getState());
		tv_carBrand.setText(order.getCar().getCarBrand());
		tv_plate.setText(order.getCar().getPlate());
		tv_orderId.setText(order.getOrderId());
		tv_state.setText(order.getState());
		tv_carBrand.setText(order.getCar().getCarBrand());
		tv_plate.setText(order.getCar().getPlate());
		tv_taketime.setText(order.getTakeTime());
		tv_returntime.setText(order.getReturnTime());
		if (order.getState().equals("待付款")) {
			tv_txt.setText("应付款");
		} else if (order.getState().equals("已付款")) {
			tv_txt.setText("实付款：");
		}
		tv_payment.setText("￥" + order.getPayment());
		img_car.setImageResource(order.getCar().getImg());
		tv_taketime.setText(order.getTakeTime());
		tv_returntime.setText(order.getReturnTime());
		if (order.getState().equals("待付款")) {
			tv_txt.setText("应付款");
		}
		tv_payment.setText("￥" + order.getPayment());
		img_car.setImageResource(order.getCar().getImg());
		bt_no = (Button) view.findViewById(R.id.no);
		bt_yes = (Button) view.findViewById(R.id.yes);
		bt_no.setOnClickListener(this);
		bt_yes.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.no:
			ll_cancle.setVisibility(View.GONE);
			break;
		case R.id.yes:
			ll_cancle.setVisibility(View.GONE);
			break;

		default:
			break;
		}
	}
}
